package com.dkd.handler;

import cn.elegent.ac.ACHandler;
import cn.elegent.ac.annotation.Topic;
import com.dkd.config.TopicConfig;
import com.dkd.config.constant.TaskType;
import com.dkd.config.constant.VmStatus;
import com.dkd.contract.TaskCompleteContract;
import com.dkd.service.VendingMachineService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 完成工单消息处理类
 */
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedMsgHandler implements ACHandler<TaskCompleteContract> {

    @Autowired
    private VendingMachineService vmService;//售货机服务

    @Override
    public void process(String topic, TaskCompleteContract taskCompleteContract) throws Exception {
        if (taskCompleteContract == null || Strings.isNullOrEmpty(taskCompleteContract.getInnerCode())) {
            return;
        }

        //如果是投放工单，将售货机修改为运营状态
        if (taskCompleteContract.getTaskType() == TaskType.TASK_TYPE_DEPLOY) {
            vmService.updateStatus(taskCompleteContract.getInnerCode(), VmStatus.VM_STATUS_RUNNING);
        }

        //如果是撤机工单，将售货机修改为撤机状态
        if (taskCompleteContract.getTaskType() == TaskType.TASK_TYPE_REVOKE) {
            vmService.updateStatus(taskCompleteContract.getInnerCode(), VmStatus.VM_STATUS_REVOKE);
        }
    }
}